/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.VisualStudio.Modeling.Shell;

namespace DslEditorPowerToy.Controls.Languages.ToolWindows
{
    /// <summary>
    /// Provides a base class toolwindow specific to a DSL designer.
    /// Only displays tool window when the specified DSL designer is active.
    /// </summary>
    [CLSCompliant(false)]
    public abstract class DslLanguageBase : ToolWindow
    {
        private ModelingDocView _docView = null;
        private Type _docViewType = null;

        #region Constructors
        public DslLanguageBase(IServiceProvider serviceProvider, Type dslLanguageDocViewType)
            : base(serviceProvider)
        {
            this._docViewType = dslLanguageDocViewType;
        }

        #endregion

        #region Protected Properties
        /// <summary>
        /// Gets the current document view
        /// </summary>
        protected ModelingDocView DocView
        {
            get
            {
                return this._docView;
            }
        }
        /// <summary>
        /// Gets whether the tool window supports undo. Since this is a DSL language it does support undo.
        /// </summary>
        protected override bool EnableUndo
        {
            get
            {
                return true;
            }
        }
        #endregion

        #region Protected Methods
        /// <summary>
        /// Handles the change in document in the IDE
        /// </summary>
        /// <param name="oldView"></param>
        /// <param name="newView"></param>
        protected override void OnDocumentWindowChanged(ModelingDocView oldView, ModelingDocView newView)
        {
            base.OnDocumentWindowChanged(oldView, newView);

            //Ensure correct language designer is active
            if (null != newView)
            {
                //Check if this instance's view
                if ((null != this._docViewType)
                    && (newView.GetType() == this._docViewType))
                {
                    //Only set DocView first time
                    if ((null == this._docView)
                        || (newView != oldView))
                        this._docView = newView;

                    //Show only if not closed by user (0 == notClosed, 1 == Closed)
                    if (0 == this.Frame.IsVisible())
                        this.ShowNoActivate();
                }
                else
                {
                    //Leaving this view, hide only if visible (0 == notClosed, 1 == Closed)
                    if (0 == this.Frame.IsVisible())
                        this.Frame.Hide();
                }
            }
            else
            {
                //Leaving this view, hide only if visible (0 == notClosed, 1 == Closed)
                if (0 == this.Frame.IsVisible())
                    this.Frame.Hide();
            }
        }
        #endregion
    }
}
